#pragma once
#include<iostream>
#include<assert.h>

using namespace std;

namespace my_list
{
	template<class T>
	struct ListNode
	{
		ListNode* _prev;
		ListNode* _next;
		T _val;

		ListNode(const T& val = T())
			:_prev(nullptr)
			,_next(nullptr)
			,_val(val)
		{}

	};

	template<class T, class Ref, class Ptr>
	struct ListIterator
	{
		typedef ListIterator<T, Ref, Ptr> Self;
		typedef ListNode<T> Node;

		Node* pNode;

	/*	ListIterator(const ListIterator& x)
		{
			pNode = x.pNode;
		}*/

		ListIterator(Node* p = nullptr)
			:pNode(p)
		{}
		

		Ref operator*()
		{
			return pNode->_val;
		}

		Ptr operator->()
		{
			return &(pNode->_val);
		}

		Self& operator++()
		{
			pNode = pNode->_next;
			return *this;
		}

		Self& operator++(int)
		{
			Self tmp = *this;
			pNode = pNode->_next;
			return tmp;
		}

		Self& operator--()
		{
			pNode = pNode->_prev;
			return *this;
		}

		Self& operator--(int)
		{
			Self tmp = *this;
			pNode = pNode->_prev;
			return tmp;
		}

		bool operator!=(const Self& x)
		{
			return pNode != x.pNode;
		}

		bool operator==(const Self& x)
		{
			return pNode == x.pNode;
		}
	};

	template<class T>
	class list
	{
		typedef ListNode<T> Node;
	public:
		typedef ListIterator<T, T&, T*> iterator;
		typedef ListIterator<T, const T&, const T*> const_iterator;


		list()
			:_pHead(nullptr)
			, _size(0)
		{
			CreateHead();
		}

		list(int n, const T& value = T())
		{
			CreateHead();

			while (n--)
			{
				push_back(value);
			}
		}

		template <class Iterator>
		list(Iterator first, Iterator last)
		{
			CreateHead();
			Iterator it = first;
			while (it != last)
			{
				push_back(*it);
				++it;
			}
		}

		list(const list<T>& l)
		{
			CreateHead();
			const_iterator it = l.begin();
			while (it != l.end())
			{
				push_back(*it);
				++it;
			}
		}
		list<T>& operator=(list<T> l)
		{
			swap(l);
			return *this;
		}
		~list()
		{
			clear();
			delete _pHead;
			_pHead = nullptr;
		}

		iterator begin()
		{
			return _pHead->_next;
		}
		iterator end()
		{
			return _pHead;
		}
		const_iterator begin() const
		{
			return _pHead->_next;
		}
		const_iterator end() const
		{
			return _pHead;
		}

		size_t size()const
		{
			return _size;
		}
		bool empty()const
		{
			return _size == 0;
		}

		T& front()                                                                                                
		{
			return _pHead->_next->_val;
		}

		T& back()
		{
			return _pHead->_prev->_val;
		}

		const T& front() const
		{
			return _pHead->_next->_val;
		}

		const T& back() const
		{
			return _pHead->_prev->_val;
		}

		void push_back(const T& val)
		{
			Node* p = new Node(val);

			p->_next = _pHead;
			_pHead->_prev->_next = p;
			p->_prev = _pHead->_prev;
			_pHead->_prev = p;
			++_size;
		}
		void pop_back()
		{
			Node* p = _pHead->_prev;

			p->_prev->_next = _pHead;
			_pHead->_prev = p->_prev;
			delete p;
			--_size;
		}
		void push_front(const T& val)
		{
			Node* p = new Node(val);

			p->_next = _pHead->_next;
			p->_prev = _pHead;
			_pHead->_next = p;
			p->_next->_prev = p;
			++_size;
		}
		void pop_front()
		{
			Node* p = _pHead->_next;

			_pHead->_next = p->_next;
			p->_next->_prev = _pHead;
			delete p;
			--_size;
		}

		iterator insert(iterator pos, const T& val)
		{
			Node* p = new Node(val);

			p->_next = pos.pNode;
			p->_prev = pos.pNode->_prev;
			pos.pNode->_prev->_next = p;
			pos.pNode->_prev = p;

			++_size;

			return pos;
		}

		iterator erase(iterator pos)
		{
			assert(pos != end());

			iterator ret = pos.pNode->_next;
			
			pos.pNode->_prev->_next = pos.pNode->_next;
			pos.pNode->_next->_prev = pos.pNode->_prev;
			delete pos.pNode;

			--_size;

			return ret;
		
		}

		void clear()
		{
			iterator it = begin();

			while (it != end())
			{
				iterator cur = it++;
				delete cur.pNode;
			}
			_pHead->_next = _pHead;
			_pHead->_prev = _pHead;

			_size = 0;
		}

		void swap(list<T>& l)
		{
			std::swap(_pHead, l._pHead);
			std::swap(_size, l._size);
		}


	private:
		Node* _pHead;
		size_t _size;

		void CreateHead()
		{
			_pHead = new Node;
			_pHead->_next = _pHead;
			_pHead->_prev = _pHead;
			_size = 0;
		}
	};
}

template<class Container>
void print_container(const Container& v)
{
	auto it = v.begin();
	while (it != v.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
}